//
//  AppDelegate.m
//  CoolTennisBall-NewBranch
//
//  Created by Coollang on 2017/8/24.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "AppDelegate.h"
#import "DataModel.h"
#import "MyNavigationViewController.h"
#import "MyTabbarViewController.h"
#import "LGBluetooth.h"
#import <Realm/Realm.h>
#import <AVFoundation/AVFoundation.h>
#import "AppDelegate+extension.h"
#import "iVersion.h"
#import <objc/runtime.h>


@interface AppDelegate ()
{
    DataModel * _dataModel;
}
@end
@implementation AppDelegate

#pragma mark - 配置版本信息
+ (void)initialize {
    //example configuration
    [iVersion sharedInstance].appStoreID = 1026489906;
    [iVersion sharedInstance].ignoreButtonLabel = NSLocalizedStringFromTable(@"Cancel", @"Location", nil);
    [iVersion sharedInstance].remindButtonLabel = @"";
    [iVersion sharedInstance].checkAtLaunch = NO;
    [iVersion sharedInstance].useAllAvailableLanguages = YES;
}

#pragma mark - Private Method
- (void)deliverDataModal
{
    _dataModel = [[DataModel alloc] init];
}

- (void)configureMobClick
{
    [MobClick startWithAppkey:@"569dffa1e0f55a841a0004f1" reportPolicy:BATCH channelId:nil];
    
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    [MobClick setAppVersion:version];
    [MobClick setEncryptEnabled:YES];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
//        NSArray *languages = [NSLocale preferredLanguages];
//        NSString *currentLanguage = [languages objectAtIndex:0];
//    
//        if (![currentLanguage hasPrefix:@"en"] && ![currentLanguage hasPrefix:@"zh-Hans"] && ![currentLanguage hasPrefix:@"fr"]) {
//            YFLanguage language = [NSBundle currentLangeuage];
//            if (language == kLanguageChineseSimple) {
//                [Languager standardLanguager].currentLanguage = @"zh-Hans";
//            }else if (language == kLanguageFrennch){
//                [Languager standardLanguager].currentLanguage = @"fr";
//            }else {
//                [Languager standardLanguager].currentLanguage = @"zh-Hant";
//            }
//        }
    [[YFNetworkingService sharedInstance] configureBaseUrl];
    
    [self deliverDataModal];
    [self configureShareSDK];
    [self configureMobClick];
    [self migrationRealm];
//    [self cleanRealmDataIfUpdateVersion];
    [LGCentralManager sharedInstance];//初始化，防止第一次搜索找不到设备
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    
    return YES;
}

/**
 打印crash信息
 */
void uncaughtExceptionHandler(NSException *exception) {
    
    NSLog(@"\n\n崩溃信息 ：reason: %@\n\n\n", exception);
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     [application beginReceivingRemoteControlEvents];
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    // 发送一个即将计入前台的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"kApplicationWillEnterForeground" object:nil];
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
